<?php
namespace Admin\Controller;

use Admin\Model\Admin;
use Common\Model\Movie;
use Common\Model\File;
use Think\Controller;

/**
 * 电影管理
 * @author  qinshan
 * @since   1.0
 */
class MovieController extends BaseController
{
    public function index()
    {
        $name=$_GET['name'];
        if(!empty($_GET['name']))
        {
            $this->assign('name',$_GET['name']);
        }
        $status=Movie::STATUS_YES;
        $count = M('Movie')->where("status=$status and name like'%$name%'")->count();
        $page = new \Think\Page($count,4);
        $page->setConfig('header','共页数');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $show = $page->show();
        //做模糊查询传值

        //分页查询电影数据
        $list = M('movie')->where("status=$status and name like'%$name%'")->order('created_at desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach($list as $k=>$v)
        {
            $f=File::sub($v['cover']).'125_'.$v['cover'];
            $list[$k]['cover']='/Public/Uploads/'.$f;
            $list[$k]['release_at']=date('Y-m-d',$v['release_at']);
            $list[$k]['created_at']=date('Y-m-d',$v['created_at']);
            $list[$k]['status']=Movie::statusAlias($v['status']);
            $list[$k]['language']=Movie::langAlias($v['language']);
            $list[$k]['tags']=Movie::tagAlias($v['tags']);
        }
        //var_dump($list);exit;
        $this->assign('active', 'm');
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }
//添加页面
    public function add()
    {
        if (!empty($_POST)) {
            //var_dump($_POST);exit;
            $model = D('movie');
            $_POST['tags']= implode(',', $_POST['tags']);
            if(!empty($_POST['name'])){
                $this->assign('name',$_POST['name']);
            }
            if(!empty($_POST['summary'])){
                $this->assign('summary',$_POST['summary']);
            }
            if(!empty($_POST['description'])){
                $this->assign('description',$_POST['description']);
            }
            if(!empty($_POST['score'])){
                if(!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $_POST['score']))
                {
                    session('message','评分数据类型不正确');
                }else{
                $this->assign('score',$_POST['score']);
                }
            }
            if(!empty($_POST['actore'])){
                $this->assign('actore',$_POST['actore']);
            }
            if(!empty($_POST['long'])){
                if(!preg_match('/^[1-9]\d*$/', $_POST['long']))
                {
                    session('message','电影时长数据类型不正确');
                }else{
                $this->assign('long',$_POST['long']);
                }
            }

            //根据表单提交的POST数据创建数据对象
            $data = $model->create();

            if (!$data) {
                // 如果创建失败表示验证没有通过 输出错误提示信息
                $message = $model->getError();
                session('message', $message);
            } else {
                $data['release_at'] = strtotime($data['release_at']);
                $filename = File::upload();
                $data['cover'] = $filename;
                if (empty($data['cover'])) {
                    session('message', '图片未上传');
                } elseif (empty($_POST['language'])) {
                        session('message', '语言未选择');
                    } elseif(empty($_POST['status'])) {
                    session('message', '状态未选择');
                }elseif($_POST['score']>10.00)
                {
                    session('message','评分范围不正确');
                }elseif(!preg_match('/^[1-9]\d*$/', $_POST['long']))
                {
                    session('message','电影时长数据类型不正确');
                }else{
                $res = $model->add($data);
                    if ($res) {
                        //添加成功
                        session('message', '添加成功');
                        $this->redirect('index');
                        return false;
                    } else {
                        File::setUnlink($filename);
                        session('message', '添加失败');
                    }
            }
        }
    }
        $this->display();
    }

    public function update()
    {
        $where['id']=$_GET['id'];
        $model = M('Movie')->where($where)->find();
        if (empty($model)) {
            throw new \Exception("id不存在");
        }
        $f=File::sub($model['cover']).'300_'.$model['cover'];
        $model['cover']='/Public/Uploads/'.$f;
        $model['tags']=explode(',',$model['tags']);
        //var_dump($model);exit;
        //var_dump($model['tags']);exit;
        $this->assign('tags',$model['tags']);
        $this->assign('model', $model);
        $this->assign('id', $_GET['id']);
        $this->display();

    }
  public function updateA()
  {
      $where['id'] = $_GET['id'];
      //var_dump($where);exit;
      if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $_POST['score'])) {
          session('message', '评分数据类型不正确');
          $this->redirect('update',['id'=>$_GET['id']]);
      } elseif (!preg_match('/^[1-9]\d*$/', $_POST['long'])) {
          session('message', '电影时长数据类型不正确');
          $this->redirect('update',['id'=>$_GET['id']]);
      } elseif(empty($_POST['tags'])){
          session('message', '标签不能为空');
          $this->redirect('update',['id'=>$_GET['id']]);
      }else{
          $data['name'] = $_POST['name'];
          $data['summary'] = $_POST['summary'];
          $data['description'] = $_POST['description'];
          $data['score'] = $_POST['score'];
          $data['actore'] = $_POST['actore'];
          $data['long'] = $_POST['long'];
          $data['release_at'] = strtotime($_POST['release_at']);

          $filename = File::upload();
          $data['cover'] = $filename;
          if (!$data['cover']) {
              $model = M('Movie')->where($where)->find();
              $data['cover'] = $model['cover'];
          }
          $data['tags'] = implode(',', $_POST['tags']);
          if (!$data['tags']) {
              $model = M('Movie')->where($where)->find();
              $data['tags'] = $model['tags'];
          }
          $data['language'] = $_POST['language'];
          $data['status'] = $_POST['status'];
          $data['updated_at'] = time();
          //var_dump($data);exit;
          $res = M('Movie')->where($where)->save($data);
          //var_dump($res);exit;
          if ($res) {
              session('message', '修改成功');
              $this->redirect('index');
              return false;
          } else {
              session('message', '修改失败');
          }

      }
  }
}